package com.learn.redis.helper;


import com.learn.core.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author PC
 * RedisTemplate实现的redisHelper
 */
public class RedisTemplateHelper implements RedisHelper {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public String strGet(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    @Override
    public <T> T tGet(String key, Class<T> tClass) {
        return JsonUtils.toObjectByClass(redisTemplate.opsForValue().get(key), tClass);
    }

    @Override
    public void strSet(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
    }

    @Override
    public Boolean strSetNx(String key, String value) {
        return redisTemplate.opsForValue().setIfAbsent(key, value);
    }

    @Override
    public void mSet(Map<String, String> multiMap) {
        redisTemplate.opsForValue().multiSet(multiMap);
    }

    @Override
    public List<String> mGet(List<String> keyList) {
        return redisTemplate.opsForValue().multiGet(keyList);
    }

    @Override
    public Long incr(String key) {
        return redisTemplate.opsForValue().increment(key);
    }

    @Override
    public Long decr(String key) {
        return redisTemplate.opsForValue().decrement(key);
    }

    @Override
    public Integer append(String key, String appendStr) {
        return redisTemplate.opsForValue().append(key, appendStr);
    }

    @Override
    public Boolean del(String key) {
        return redisTemplate.delete(key);
    }

    @Override
    public Long mDel(List<String> keyList) {
        return redisTemplate.delete(keyList);
    }

    @Override
    public Long strlen(String key) {
        return redisTemplate.opsForValue().size(key);
    }

    @Override
    public String getSet(String key, String value) {
        return redisTemplate.opsForValue().getAndSet(key, value);
    }

    @Override
    public Long lPush(String key, String value) {
        return redisTemplate.opsForList().leftPush(key, value);
    }

    @Override
    public Long rPush(String key, String value) {
        return redisTemplate.opsForList().rightPush(key, value);
    }

    @Override
    public String lPop(String key) {
        return redisTemplate.opsForList().leftPop(key);
    }

    @Override
    public String rPop(String key) {
        return redisTemplate.opsForList().rightPop(key);
    }

    @Override
    public List<String> lRange(String key, Long startIndex, Long endIndex) {
        return redisTemplate.opsForList().range(key, startIndex, endIndex);
    }

    @Override
    public Long sAdd(String key, String... values) {
        return redisTemplate.opsForSet().add(key, values);
    }

    @Override
    public Long sRem(String key, Object... values) {
        return redisTemplate.opsForSet().remove(key, values);
    }

    @Override
    public Set<String> sMembers(String key) {
        return redisTemplate.opsForSet().members(key);
    }

    @Override
    public Long sCard(String key) {
        return redisTemplate.opsForSet().size(key);
    }

    @Override
    public Boolean zAdd(String key, String member, Double score) {
        return redisTemplate.opsForZSet().add(key, member, score);
    }

    @Override
    public Long zRem(String key, Object... members) {
        return redisTemplate.opsForZSet().remove(key, members);
    }

    @Override
    public Set<String> zRange(String key, Long startIndex, Long endIndex) {
        return redisTemplate.opsForZSet().range(key, startIndex, endIndex);
    }

    @Override
    public void hSet(String key, Object hashKey, Object value) {
        redisTemplate.opsForHash().put(key, hashKey, value);
    }

    @Override
    public Object hGet(String key, Object hashKey) {
        return redisTemplate.opsForHash().get(key, hashKey);
    }

    @Override
    public Long hDel(String key, Object... hashKeys) {
        return redisTemplate.opsForHash().delete(key, hashKeys);
    }

    @Override
    public Boolean expire(String key, Long timeOut, TimeUnit timeUnit) {
        return redisTemplate.expire(key, timeOut, timeUnit);
    }

    @Override
    public Long ttl(String key, TimeUnit timeUnit) {
        return redisTemplate.getExpire(key, timeUnit);
    }

    @Override
    public void publish(String channelName, Object message) {
        redisTemplate.convertAndSend(channelName, message);
    }

}
